//
//  setNewsView.m
//  tcpoctest
//
//  Created by apple on 13-6-26.
//  Copyright (c) 2013年 gump. All rights reserved.
//

#import "setNewsView.h"
#import <QuartzCore/QuartzCore.h>

#import "sendPersonDistroble.h"

@implementation setNewsView

-(id)init
{
    self = [super init];
    
    if (self != nil) {
        
    }
    
    return self;
}

-(void)initUI
{
    [self createTitle];
    self.mtitle.text = @"新消息通知";
    
    UIBarButtonItem *backItem = [self createBarBackButtonItemWithTitle:@"返回" target:self action:@selector(backButtonPressed)];
    self.navItem.leftBarButtonItem = backItem;
    
    mtablev =[[UITableView alloc]initWithFrame:CGRectMake(0, TOPBAR_HIGHT, WINDOW_WIDTH, WINDOW_HIGTH - TOPBAR_HIGHT - 20)
                                         style:UITableViewStyleGrouped];
    mtablev.backgroundColor = CELL_BG_COLOR;
    mtablev.separatorStyle=UITableViewCellSeparatorStyleNone;
    mtablev.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgColor.png"]];
    mtablev.backgroundView = nil;
    mtablev.delegate =self;
    mtablev.dataSource =self;
    [self.view addSubview:mtablev];
}

-(void)backButtonPressed
{
    [[ViewManager sharedViewManager] backView:self];
}

#pragma mark - tableView DataSource -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
            case 2:
                case 4:
            {
                return 44;
            }
                break;
                case 1:
            {
                return 100;
            }
                break;
                case 3:
            case 5:
            {
                return 60;
            }
                break;
            default:
                return 44;
                break;
        }
    }else{
        return 44;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 6;
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%d%d",
                                indexPath.section,
                                indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.imageView.image = nil;
    
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
            {
                [cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell.png"]]];
                UIImageView *selectImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellpress.png"]];
                [cell setSelectedBackgroundView:selectImageView];
                
                cell.textLabel.text = @"接受新消息通知";
                cell.detailTextLabel.text = @"已开启";
            }
                break;
                case 1:
            {
                cell.textLabel.text = nil;
                cell.detailTextLabel.text = [NSString stringWithFormat:
                                             @"如果你要关闭或开启%@的新消息通知，请在\"设置\"-\"通知\"功能中,找到应用程序\"%@\"进行修改",
                                             T_PROJECTNAME,
                                             T_PROJECTNAME];
                cell.detailTextLabel.numberOfLines = 0;
                cell.detailTextLabel.textAlignment = NSTextAlignmentLeft;
                
                cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
            }
                break;
            case 2:
            {
                [cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell.png"]]];
                UIImageView *selectImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellpress.png"]];
                [cell setSelectedBackgroundView:selectImageView];
                
                cell.detailTextLabel.text = nil;
                cell.textLabel.text = @"消息免打扰";
                if (!CheckSwitch) {
                    CheckSwitch = [[ UISwitch alloc]initWithFrame:CGRectMake(190,15,0.0,0.0)];
                    [CheckSwitch addTarget: self action:@selector(CheckswitchValueChanged)
                          forControlEvents:UIControlEventValueChanged];
                    CheckSwitch.on = [DataControl shareControl].personDS.uinfo.cDisturb == SDT_OPEN ? YES:NO;
                    [cell setAccessoryView:CheckSwitch];
                }
            }
                break;
            case 3:
            {
                cell.textLabel.text = nil;
                cell.detailTextLabel.text = @"开启夜间免打扰有效时段22:00~8:00";
                cell.detailTextLabel.numberOfLines = 0;
                cell.detailTextLabel.textAlignment = NSTextAlignmentLeft;
                
                cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];

            }
                break;
                case 4:
            {
                [cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell.png"]]];
                UIImageView *selectImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellpress.png"]];
                [cell setSelectedBackgroundView:selectImageView];
                
                cell.accessoryType = UITableViewCellAccessoryNone;
                cell.detailTextLabel.text = nil;
                cell.textLabel.text = @"声音";
                if (!SoundSwitch) {
                    SoundSwitch = [[ UISwitch alloc]initWithFrame:CGRectMake(190,15,0.0,0.0)];
                    [SoundSwitch addTarget: self action:@selector(SoundSwitchValueChanged)
                          forControlEvents:UIControlEventValueChanged];
                    SoundSwitch.on = [DataControl shareControl].personDS.uinfo.bSound ? YES:NO;
                    [cell setAccessoryView:SoundSwitch];
                }
            }
                break;
            case 5:
            {
                cell.textLabel.text = nil;
                cell.detailTextLabel.text = [NSString stringWithFormat:@"当%@在运行时，你可以设置是否需要声音或者振动"
                                             ,T_PROJECTNAME];
                cell.detailTextLabel.numberOfLines = 0;
                cell.detailTextLabel.textAlignment = NSTextAlignmentLeft;
                
                cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
            }
                break;
            default:
                break;
        }
        
    }
    
    return cell;
}

#pragma mark -table delegate-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -private fun-
-(void)CheckswitchValueChanged
{
    if (CheckSwitch.on == YES) {
        [DataControl shareControl].personDS.uinfo.cDisturb = SDT_OPEN;
        
        [[netManager sharedNetManager].sendPersonDisTro SendDisTro:SDT_OPEN];
    }
    else
    {
        [DataControl shareControl].personDS.uinfo.cDisturb = SDT_CLOSE;
        
        [[netManager sharedNetManager].sendPersonDisTro SendDisTro:SDT_CLOSE];
    }
    
    [[DataControl shareControl] SetUpdateUserType:UUT_DISTRO];
    [[DataControl shareControl].LCDS addLogicCommand:LC_UPDATEUIDB];
}

-(void)SoundSwitchValueChanged
{
    if (SoundSwitch.on == YES) {
        [DataControl shareControl].personDS.uinfo.bSound = true;
    }
    else
    {
        [DataControl shareControl].personDS.uinfo.bSound = false;
    }
    
    [[DataControl shareControl] SetUpdateUserType:UUT_SOUND];
    [[DataControl shareControl].LCDS addLogicCommand:LC_UPDATEUIDB];
}

-(void)viewWillShow
{
    [self refreshView];
}

-(void)refreshView
{
    if ([DataControl shareControl].personDS.uinfo.cDisturb == SDT_OPEN) {
        CheckSwitch.on = YES;
    }
    else if([DataControl shareControl].personDS.uinfo.cDisturb == SDT_CLOSE)
    {
        CheckSwitch.on = NO;
    }
    
    if ([DataControl shareControl].personDS.uinfo.bSound) {
        SoundSwitch.on = YES;
    }
    else 
    {
        SoundSwitch.on = NO;
    }
}

@end
